package com.cool4j.framework.ds.impl;

import org.apache.commons.dbcp.BasicDataSource;

/**
 * 默认数据源工厂类
 * <br/>
 * 基于 Apache Commons DBCP 实现
 *
 * @author Frank create on 2016/1/4 14:59
 */
public class DefaultDataSourceFactory extends AbstractDataSourceFactory<BasicDataSource> {

  @Override
  public BasicDataSource createDataSource() {
    return new BasicDataSource();
  }

  @Override
  public void setDriver(BasicDataSource ds, String driver) {
    ds.setDriverClassName(driver);
  }

  @Override
  public void setUrl(BasicDataSource ds, String url) {
    ds.setUrl(url);
  }

  @Override
  public void setUsername(BasicDataSource ds, String username) {
    ds.setUsername(username);
  }

  @Override
  public void setPassword(BasicDataSource ds, String passwrod) {
    ds.setPassword(passwrod);
  }

  @Override
  public void setAdvancedConfig(BasicDataSource ds) {
    // 解决 java.sql.SQLException: Already closed. 的问题（连接池会自动关闭长时间没有使用的连接）
    ds.setValidationQuery("select 1 from dual");
  }
}
